package ace.module.account.core.impl.service.authentication.handler.impl;

import ace.cmp.core.exception.BusinessException;
import ace.module.account.api.model.auth.input.AuthenticationParams;
import ace.module.account.core.impl.service.authentication.handler.AuthenticationHandler;
import ace.module.account.core.impl.service.authentication.handler.AuthenticationHandlerManager;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/3/16 9:33 管理 {@link AuthenticationHandler}
 */
@Component
@AllArgsConstructor
@Slf4j
public class AuthenticationHandlerManagerImpl implements AuthenticationHandlerManager {

  private final List<AuthenticationHandler> authenticationHandlers;

  @Override
  public <T extends AuthenticationParams> AuthenticationHandler<T> get(String grantType) {
    return authenticationHandlers.stream()
        .filter(p -> p.getGrantType().equals(grantType))
        .findFirst()
        .orElseThrow(() -> new BusinessException("不支持认证类型," + grantType));
  }
}
